package com.zzb.weikeautosign.user;

import com.zzb.weikeautosign.Constants;
import com.zzb.weikeautosign.RedisUtil;
import com.zzb.weikeautosign.threadpool.ExcuteThread;
import com.zzb.weikeautosign.threadpool.ThreadQueue;
import org.joda.time.DateTime;
import redis.clients.jedis.Jedis;

import java.util.Date;

/**
 * 用户管理
 * Created by zzb on 16/2/20.
 */
public class UserManager {
    private  static UserManager manage;
    public static UserManager getInstance() {
        if(manage == null ) manage = new UserManager();
        return manage;
    }

    /**
     * 新增用户
     * @param username 用户名
     * @param password 密码
     * @param type 类型 0 -> 免费用户 , 1 -> 一个月 , 2 -> 一学期 , 3 -> 一学年
     */
    public static void add(String username , String password , int type){
        Jedis jedis = RedisUtil.getJedis();
        //以免费用户的形式存入redis
        if (jedis != null) {
            jedis.hset(Constants.USERTYPEKEYS[type],username,username+","+password+","+new DateTime().getMillis());
        }
        //立刻加入线程池
        ThreadQueue.getInstances().add(new ExcuteThread(username,password));
    }

    /**
     * 删除用户
     * @param username 用户名
     */
    public static void del(String username){
        Jedis jedis = RedisUtil.getJedis();
        if(jedis!=null)
        for(String key : Constants.USERTYPEKEYS){
            if(jedis.hexists(key,username)){
                jedis.hdel(key,username);
            }
        }
    }
}
